<template>
    <div class="container">
        <div class="row justify-content-center mt-5">
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        登录
                    </div>
                    <div class="card-body">
                            <div class="form-group">
                                <label for="username">用户名</label>
                                <input type="text" class="form-control" v-model="username" required>
                            </div>
                            <div class="form-group">
                                <label for="password">密码</label>
                                <input type="password" class="form-control" v-model="password" required>
                            </div>
                            <ButtonFill class="btn btn-primary" :enable="true" content="登录" v-on:onClick="onLogin" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import {login} from '@/api/user'
import { Toast } from "mint-ui";
import { setCacheUserInfo } from "@/utils/token";
import ButtonFill from "@/components/ButtonFill";

export default {
    data(){
        return {
            username:'',
            password:'',
        }
    },
    components: {ButtonFill},
    methods:{
        async onLogin() {
            let data = await login({
                phone: this.username,
                pwd: this.password,
            });
            if (data.code == 200) {
                let userInfo = data.data;

                //alert(userInfo['userName']);
                setCacheUserInfo(userInfo);
                if (window.history.length > 1) {
                    // this.$router.go(-1);
                    this.$router.replace("/");
                } else {
                    this.$router.replace("/");
                }
            } else {
                Toast({
                    message: data.msg,
                    position: "center",
                });
            }
        }
    }
}
</script>
